/**
 * 
 */
package demo.tellurium.test.cqp;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import demo.tellurium.framework.BaseTelluriumJUnitTestCase;
import demo.tellurium.module.cqp.CQPAllDocumentsModule;
import demo.tellurium.module.cqp.CQPLoginModule;
import demo.tellurium.module.cqp.CQPProfileModule;

/**
 * @author roger.xu
 * 
 */
public class CreateAssessmentJUnitTestCase extends BaseTelluriumJUnitTestCase {
    private static CQPLoginModule cqpLogin;
    private static CQPAllDocumentsModule cqpAllDocuments;
    private static CQPProfileModule cqpProfile;

    @BeforeClass
    public static void initUi() {
        cqpLogin = new CQPLoginModule();
        cqpLogin.defineUi();

        cqpAllDocuments = new CQPAllDocumentsModule();
        cqpAllDocuments.defineUi();

        cqpProfile = new CQPProfileModule();
        cqpProfile.defineUi();
    }

    @Before
    public void waitDelay() {
        waitingDelay();
    }

    @Test
    public void connectTo() {
        String contextUrl = getProperty("cqp.context.url");
        String pagePath = getProperty(CQPAllDocumentsModule.PATH_KEY);
        String url = String.format("%s/%s", contextUrl, pagePath);
        connectUrl(url);
    }

    @Test
    public void testLogin() {
        console.debug("[testLogin]");
        // cqpLogin.doAdminLogin();
        String username = getProperty("cqp.login.username");
        String password = getProperty("cqp.login.password");
        cqpLogin.doLogin(username, password);
        pauseSec(3);
    }

    @Test
    public void testCreateAssessment() {
        console.debug("[testCreateAssessment]");

        for (int i = 0; i < 2; i++) {
            run();
        }
    }

    private void run() {
        cqpAllDocuments.doClickCreateAssessmentButton();
        pauseSec(3);
        createAssessment();
        pauseSec(3);
        cqpProfile.doClickCancelButton();
        pauseSec(3);
    }

    private void createAssessment() {
        String docName = String.format("test - %s", RandomStringUtils.randomAlphanumeric(8));

        int offeringTypeIndex = RandomUtils.nextInt(4) + 1;
        switch (offeringTypeIndex) {
        case 1:
            cqpProfile.doCreateSWGAssessment(docName);
            break;
        case 2:
            cqpProfile.doCreateSTGAssessment(docName);
            break;
        case 3:
            cqpProfile.doCreateStackAssessment(docName);
            break;
        case 4:
            cqpProfile.doCreateComponentAssessment(docName);
            break;
        default:
        }
    }

}
